package com.wf.service.articleServiceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wf.dao.mapper.CommentsMapper;
import com.wf.dao.pojo.Comment;
import com.wf.dao.pojo.SysUser;
import com.wf.service.CommentsService;
import com.wf.service.SysUserService;
import com.wf.utils.UserThreadLocal;
import com.wf.vo.CommentVo;
import com.wf.vo.Result;
import com.wf.vo.UserVo;
import com.wf.vo.params.CommentParam;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentsServiceImpl implements CommentsService {

    @Autowired
    private CommentsMapper commentMapper;
    @Autowired
    private SysUserService sysUserService;


    @Override
    public Result comments(Integer id) {
        //select * from ms_comment where id = {id} and level = 1
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getArticleId,id);
        queryWrapper.eq(Comment::getLevel,1);
        List<Comment> comments = commentMapper.selectList(queryWrapper);
        return Result.success(copyList(comments));
    }

    @Override
    public Result comment(CommentParam commentParam) {
        //从线程池中拿到登录用户
        SysUser sysUser = UserThreadLocal.get();
        //评论对象
        Comment comment = new Comment();
        comment.setArticleId(commentParam.getArticleId());
        comment.setAuthorId(sysUser.getId());
        comment.setContent(commentParam.getContent());
        comment.setCreateDate(System.currentTimeMillis());
        //判断评论的层级
        Integer parent = commentParam.getParent();
        if (parent == null || parent == 0) {
            comment.setLevel(1);
        }else{
            comment.setLevel(2);
        }
        comment.setParentId(parent == null ? 0 : parent);
        Integer toUserId = commentParam.getToUserId();
        comment.setToUid(toUserId == null ? 0 : toUserId);
        //将评论写入数据库
        commentMapper.insert(comment);
        return Result.success(null);
    }

    private List<CommentVo> copyList(List<Comment> comments) {
        List<CommentVo> commentVoList = new ArrayList<>();
        for (Comment comment : comments) {
            commentVoList.add(copy(comment));
        }
        return commentVoList;
    }

    private CommentVo copy(Comment comment) {
        //创建视图对象
        CommentVo commentVo = new CommentVo();
        //使用 Spring Boot 的 BeanUtils 来将 comment 深拷贝到 commentVo
        BeanUtils.copyProperties(comment,commentVo);
        //拷贝后，进行时间格式化
        commentVo.setCreateDate(new DateTime(comment.getCreateDate()).toString("yyyy-MM-dd HH:mm"));
        //设置 CommentVo 的 userVo 属性
        Integer authorId = comment.getAuthorId();
        UserVo userVo = sysUserService.findUserVoById(authorId);
        commentVo.setAuthor(userVo);
        //评论的评论
        List<CommentVo> commentVoList = findCommentsByParentId(comment.getId());
        //将评论的评论添加到该评论的视图中
        commentVo.setChildrens(commentVoList);
        //如果当前评论是属于另一个评论，就将指向的评论设置到 commentVo 中
        if (comment.getLevel() > 1) {
            Integer toUid = comment.getToUid();
            UserVo toUserVo = sysUserService.findUserVoById(toUid);
            commentVo.setToUser(toUserVo);
        }
        return commentVo;
    }

    private List<CommentVo> findCommentsByParentId(Integer id) {
        //select * from ms_commet where id = {id} and level = 2
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getParentId,id);
        queryWrapper.eq(Comment::getLevel,2);
        List<Comment> comments = commentMapper.selectList(queryWrapper);
        //将 Comment 集合转换为 CommentVo 集合后返回
        return copyList(comments);
    }


}
